package com.scqkzqtz.basesdk.utils;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.LinearInterpolator;

/**
 * Created by hl on 2018/1/10.
 */

public class ClickRadioButton extends android.support.v7.widget.AppCompatRadioButton {

    private Animator anim1, anim2, anim3, anim4, anim5, anim6;
    private int mHeight;
    private int mWidth;
    private float mX, mY;

    private ButtonClickListener listener;

    private Context context;

    public ClickRadioButton(Context context) {
        super(context);
    }

    public ClickRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public ClickRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHeight = getHeight() - getPaddingTop() - getPaddingBottom();
        mWidth = getWidth() - getPaddingLeft() - getPaddingRight();
        mX = getX();
        mY = getY();
    }

    private void init() {


        PropertyValuesHolder valueHolder_1 = PropertyValuesHolder.ofFloat(
                "scaleX", 1f, 1.25f);
        PropertyValuesHolder valuesHolder_2 = PropertyValuesHolder.ofFloat(
                "scaleY", 1f, 1.25f);
        anim1 = ObjectAnimator.ofPropertyValuesHolder(this, valueHolder_1,
                valuesHolder_2);
        anim1.setDuration(90);
        anim1.setInterpolator(new LinearInterpolator());

        anim1.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                anim2.end();
                anim3.end();
                anim4.end();
                anim5.end();
                anim6.end();
            }

            @Override
            public void onAnimationEnd(Animator animator) {

            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

        PropertyValuesHolder valueHolder_3 = PropertyValuesHolder.ofFloat(
                "scaleX", 1.25f, 1f);
        PropertyValuesHolder valuesHolder_4 = PropertyValuesHolder.ofFloat(
                "scaleY", 1.25f, 1f);
        anim2 = ObjectAnimator.ofPropertyValuesHolder(this, valueHolder_3,
                valuesHolder_4);
        anim2.setDuration(90);
        anim2.setInterpolator(new LinearInterpolator());

        anim2.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                anim3.start();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

        PropertyValuesHolder valueHolder_5 = PropertyValuesHolder.ofFloat(
                "scaleX", 1f, 1.09f);
        PropertyValuesHolder valuesHolder_6 = PropertyValuesHolder.ofFloat(
                "scaleY", 1f, 1.09f);
        anim3 = ObjectAnimator.ofPropertyValuesHolder(this, valueHolder_5,
                valuesHolder_6);
        anim3.setDuration(90);
        anim3.setInterpolator(new LinearInterpolator());

        anim3.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                anim4.start();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

        PropertyValuesHolder valueHolder_7 = PropertyValuesHolder.ofFloat(
                "scaleX", 1.09f, 1f);
        PropertyValuesHolder valuesHolder_8 = PropertyValuesHolder.ofFloat(
                "scaleY", 1.09f, 1f);
        anim4 = ObjectAnimator.ofPropertyValuesHolder(this, valueHolder_7,
                valuesHolder_8);
        anim4.setDuration(90);
        anim4.setInterpolator(new LinearInterpolator());

        anim4.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                anim5.start();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

        PropertyValuesHolder valueHolder_9 = PropertyValuesHolder.ofFloat(
                "scaleX", 1f, 1.04f);
        PropertyValuesHolder valuesHolder_10 = PropertyValuesHolder.ofFloat(
                "scaleY", 1f, 1.04f);
        anim5 = ObjectAnimator.ofPropertyValuesHolder(this, valueHolder_9,
                valuesHolder_10);
        anim5.setDuration(50);
        anim5.setInterpolator(new LinearInterpolator());

        anim5.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                anim6.start();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

        PropertyValuesHolder valueHolder_11 = PropertyValuesHolder.ofFloat(
                "scaleX", 1.04f, 1f);
        PropertyValuesHolder valuesHolder_12 = PropertyValuesHolder.ofFloat(
                "scaleY", 1.04f, 1f);
        anim6 = ObjectAnimator.ofPropertyValuesHolder(this, valueHolder_11,
                valuesHolder_12);
        anim6.setDuration(50);
        anim6.setInterpolator(new LinearInterpolator());
    }

    public void setButtonClickListener(ButtonClickListener clickListener) {
        this.listener = clickListener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                anim1.start();
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                anim1.end();
                anim2.start();
                if (listener != null) {
                    listener.onClick();
                }
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return super.onTouchEvent(event);
    }

    //按下的点是否在View内
    protected boolean innerButtonView(float x, float y) {

        if (x >= mX && y <= mX + mWidth) {
            if (y >= mY && y <= mY + mHeight) {
                return true;
            }
        }
        return false;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        //获取设置的图片
        Drawable[] drawables = getCompoundDrawables();
        if (drawables != null) {
            //第一个是left
            Drawable drawableLeft = drawables[0];
            if ( drawableLeft != null ) {
                //获取文字的宽度
                float textWidth = getPaint().measureText(getText().toString());
                int drawablePadding = getCompoundDrawablePadding();
                int drawableWidth = 0;
                drawableWidth = drawableLeft.getIntrinsicWidth();
                float bodyWidth = textWidth + drawableWidth + drawablePadding;
                int y = getWidth();
                canvas.translate((getWidth() - bodyWidth) / 2, 0);
            }
        }
        super.onDraw(canvas);
    }
    /**
     * 点击事件处理回调
     *
     * @author renzhiqiang
     */
    public interface ButtonClickListener {
        public void onClick();
    }

    @Override
    protected void onDetachedFromWindow() {
        // TODO Auto-generated method stub
        super.onDetachedFromWindow();
    }

}